package com.ple.app.importdata;

import android.app.DatePickerDialog;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.app.Activity;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.SimpleAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.ple.app.jzsf.MainActivity;
import com.ple.app.jzsf.R;
import com.ple.app.suggest.SeedActivity;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Cultivate_Activity extends Activity {

    private Spinner addcultivate_crop;
    private Spinner addcultivate_irri;
    private EditText addculitivate_manure;
    private EditText addcultivate_seed;
    private EditText addcultivate_data;
    private EditText addcultivate_total;
    private EditText addcultivate_dis;
    private EditText addcultivate_deep;
    private EditText addcultivate_yield;
    private EditText addculitivateirri_numble;
   private ImageButton add_times;
    private LinearLayout addLinearlayout;
    private ImageButton add_manuretimes;
    private LinearLayout addLinearlayoutm;
    private Button ok;
    private Button no;

    /**
     * 临时数据
     */

    private    final String[] dialog_crop_type={"玉米","小麦","水稻","大豆","棉花" };
    private  final String[] irri_type={"沟灌","浇灌","喷灌","滴灌","渗灌"};
    private  final String[] culitivate_conlm={"ID","crop","seed","date","total" ,"distance","deep","yield",
            "how_irr","irr_number","manure_number"};

    private String crop_type;
    private String option_irri;
    private int id=0;//添加方案的id
    private DatePickerDialog datePickerDialog;
    private ArrayAdapter<String> type_adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cultivate);
       inidata();
        irrigation();
         add();

    }//ocreat

    /**
     *灌溉和施肥次数
     */
    private void irrigation(){
        /**
         * 添加灌溉次数
         */
        add_times.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int i =1+Integer.valueOf(addculitivateirri_numble.getText().toString());
                String string=String.valueOf(i);
                String times="第"+string+"次灌溉时间";
                addculitivateirri_numble.setText(string);
                addLinearLayout( addLinearlayout,times);
            }
        });
        /**
         * 施肥
         */
        add_manuretimes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int i =1+Integer.valueOf(addculitivate_manure.getText().toString());
                String string=String.valueOf(i);
                String times="第"+string+"次施肥时间";
                addculitivate_manure.setText(string);
                addLinearLayout( addLinearlayoutm,times);
            }
        });
    }

    /**
     *
     */
    private void inidata(){
        addcultivate_crop= (Spinner) findViewById(R.id.addcultivate_crop);
        addcultivate_irri= (Spinner) findViewById(R.id.addcultivate_irri);
         addculitivate_manure= (EditText) findViewById(R.id.addculitivate_manure);
        addcultivate_seed= (EditText) findViewById(R.id.addcultivate_seed);
        addcultivate_data=(EditText)findViewById(R.id.addcultivate_data);
         addcultivate_total=(EditText)findViewById(R.id.addcultivate_total);
         addcultivate_dis=(EditText)findViewById(R.id.addcultivate_dis);
         addcultivate_deep=(EditText)findViewById(R.id.addcultivate_deep);
         addcultivate_yield=(EditText)findViewById(R.id.addcultivate_yield);
        addculitivateirri_numble= (EditText) findViewById(R.id.irrnumber);

        ok= (Button) findViewById(R.id.ok);
        no= (Button) findViewById(R.id.no);
        add_times= (ImageButton) findViewById(R.id.add_times);
        addLinearlayout= (LinearLayout) findViewById(R.id.addLinearlayout);
        add_manuretimes= (ImageButton) findViewById(R.id.add_manuretimes);
        addLinearlayoutm= (LinearLayout) findViewById(R.id.addLinearlayoutm);
        /**
         * spinner
         */
        addcultivate_crop.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                crop_type=dialog_crop_type[position];
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        type_adapter=new ArrayAdapter<String>(Cultivate_Activity.this,android.R.layout.simple_spinner_item,irri_type);
        addcultivate_irri.setAdapter(type_adapter);
        addcultivate_irri.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                option_irri=irri_type[position];//灌溉
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        /**
         * 添加信息
         */
        addcultivate_seed.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(Cultivate_Activity.this, SeedActivity.class);
                // startActivity(intent);
                startActivityForResult(intent,2);
            }
        });
        addcultivate_data.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Calendar calendar=Calendar.getInstance();
                datePickerDialog=new DatePickerDialog(Cultivate_Activity.this, new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                        int month1=month+1;
                        String data_text = year + "-" + month1+ "-" + dayOfMonth;
                        addcultivate_data.setText(data_text);
                    }
                },calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH));
                datePickerDialog.show();
            }
        });
        /**
         * 生成id
         */
        MyopenHelper myopenhelper = new MyopenHelper(Cultivate_Activity.this, "seed.db", null, 1);
        SQLiteDatabase db = myopenhelper.getReadableDatabase();
        Cursor curor_getid=db.query("cultivate",culitivate_conlm,null,null,null,null,null,null);
        String curor_id ="0";
        if(curor_getid.getCount()!=0&&curor_getid.moveToLast())curor_id=curor_getid.getString(0);
        curor_getid.close();
        id=1+Integer.valueOf(curor_id);
        db.close();
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(data!=null){
            if(requestCode==2&&resultCode==0){
                Bundle bundle=data.getExtras();
                String name=bundle.getString("name");
                addcultivate_seed.setText(name);
            }
        }
        else{return;}
    }
    /**
     * 添加种子信息
     */
    private void add(){
        ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String crop_type1= crop_type;
                String seed_value=addcultivate_seed.getText().toString();
                String data_value=addcultivate_data.getText().toString();
                String total_value=addcultivate_total.getText().toString();
                String dis_value=addcultivate_dis.getText().toString();
                String deep_value=addcultivate_deep.getText().toString();
                String yield_value=addcultivate_yield.getText().toString();
                String option_irri1=option_irri;
                String option_manure1=addculitivate_manure.getText().toString();
                String numble=addculitivateirri_numble.getText().toString();

                MyopenHelper myopenhelper = new MyopenHelper(Cultivate_Activity.this, "seed.db", null, 1);
                SQLiteDatabase db = myopenhelper.getReadableDatabase();
                ContentValues values = new ContentValues();
                values.put("ID",id);
                values.put("crop", crop_type1);
                values.put("seed", seed_value);
                values.put("date", data_value);
                values.put("total",total_value);
                values.put("distance", dis_value);
                values.put("deep", deep_value);
                values.put("yield", yield_value);
                values.put("how_irr", option_irri1);
                values.put("manure_number", option_manure1);
                values.put("irr_number",numble);
                db.insert("cultivate", null, values);
                db.close();
                Toast.makeText(getApplicationContext(),"已添加",Toast.LENGTH_LONG).show();
                Cultivate_Activity.this.finish();
                //Intent toImportIntent = new Intent(Cultivate_Activity.this, Input_Activity.class);
                //startActivity(toImportIntent);
            }
        });
        no.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Cultivate_Activity.this.finish();
            }
        });
    }

    /**
     * choose date
     */
    private void date(final TextView editText, final String times){
        editText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Calendar calendar=Calendar.getInstance();
                datePickerDialog=new DatePickerDialog(Cultivate_Activity.this, new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                        int month1=month+1;
                        String data_text = year + "-" + month1+ "-" + dayOfMonth;
                        editText.setText(data_text);
                        MyopenHelper myopenhelper = new MyopenHelper(Cultivate_Activity.this, "seed.db", null, 1);
                        SQLiteDatabase db = myopenhelper.getReadableDatabase();
                        String[]args=new String[]{String.valueOf(id),times};
                        String[] cul_date=new String[]{"ID","operation","date"};
                        Cursor curor=db.query("cul_date",cul_date,"ID=? and operation=?",args,null,null,null,null);
                        ContentValues values = new ContentValues();
                        if(curor.getCount()>0) {
                            values.put("date", data_text);
                            db.update("cul_date",values,"ID=? and operation=?",args);
                        }
                        else{
                            values.put("ID",id);
                            values.put("operation", times);
                            values.put("date", data_text);
                            db.insert("cul_date", null, values);}
                        curor.close();
                        db.close();
                    }
                },calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH));
                datePickerDialog.show();
            }
        });
}
/**
 * 添加控件
 */
    private void addLinearLayout(LinearLayout linearLayout, final String times){
        LinearLayout layout=new LinearLayout(this);
        LinearLayout.LayoutParams Lparams=new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
        LinearLayout.LayoutParams Tparams=new LinearLayout.LayoutParams(0,ViewGroup.LayoutParams.WRAP_CONTENT,1);
        layout.setLayoutParams(Lparams);
        layout.setPadding(0,2,0,0);
        layout.setOrientation(LinearLayout.HORIZONTAL);

        TextView textView=new TextView(this);
        textView.setLayoutParams(Tparams);
        textView.setGravity(Gravity.CENTER);
        textView.setText(times);

        final TextView editText=new EditText(this);
        editText.setLayoutParams(Tparams);
        editText.setGravity(Gravity.CENTER);
        editText.setBackgroundColor(getResources().getColor(R.color.green3));
        editText.setText("......");
        editText.setFocusable(false);
        /**
         * 设置日期选择
         */
        date(editText,times);

        layout.addView(textView);
        layout.addView(editText);
        linearLayout.addView(layout);
    }
}
